package com.zch.news.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zch.news.dao.NewsDao;
import com.zch.news.entity.News;
import com.zch.news.util.CloseObject;
import com.zch.news.util.DBConnection;

public class NewsDaoImpl implements NewsDao {

	/**
	 * 添加新闻信息
	 */
	@Override
	public boolean addNews(News news) {
		// 获得数据库对象
		Connection conn = DBConnection.getConnection();
		String sql = "insert into news_table(no,title,content,author,createTime) values(?,?,?,?,?)";
		// 初始化一个PreparedStatement对象
		PreparedStatement ps = null;
		// 初始化数据库中被操作的数据行数
		int i = 0;
		try {
			ps = conn.prepareStatement(sql);
			// 向数据库中添加信息
			ps.setInt(1, news.getNo());
			ps.setString(2, news.getTitle());
			ps.setString(3, news.getContent());
			ps.setString(4, news.getAuthor());
			ps.setString(5, news.getCreatTime());
			// 数据库中被操作的数据行数b
			i = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseObject.close(conn, ps);
		}
		if (i == 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean deleteNews(int no) {
		// 获得数据库连接对象
		Connection conn = DBConnection.getConnection();
		String sql = "delete news_table where no=?";
		PreparedStatement ps = null;
		int i = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, no);
			i = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseObject.close(conn, ps);
		}
		if (i == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 更新新闻信息
	 */
	@Override
	public boolean updateNews(News news) {
		// 获得数据库连接对象
		Connection conn = DBConnection.getConnection();
		String sql = "update news_table set title=?,content=?,author=?,createTime=? where no=?";
		PreparedStatement ps = null;
		int i = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, news.getTitle());
			ps.setString(2, news.getContent());
			ps.setString(3, news.getAuthor());
			ps.setString(4, news.getCreatTime());
			ps.setInt(5, news.getNo());
			i = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭流对象
			CloseObject.close(conn, ps);
		}
		if (i == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取所有新闻
	 */
	@Override
	public List<News> getAllNews() {
		// 存放从数据库中获取的所有新闻信息
		List<News> list = new ArrayList<News>();
		Connection conn = DBConnection.getConnection();
		String sql = "select * from news_table";
		// 初始化PreparedStatement对象
		PreparedStatement ps = null;
		// 初始化ResultSet对象
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			// 得到一个结果集
			rs = ps.executeQuery();
			// 对结果集进行处理
			while (rs.next()) {// next()数组存在则返回true，否则返回false
				// 实例化一个User对象
				News news = new News();
				news.setNo(rs.getInt("no"));
				news.setTitle(rs.getString("title"));
				news.setContent(rs.getString("content"));
				news.setAuthor(rs.getString("author"));
				news.setCreatTime(rs.getString("createTime"));
				// 向集合中添加用户信息
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭流对象
			CloseObject.close(conn, ps, rs);
		}
		return list;
	}

	/**
	 * 获得一则新闻
	 */
	@Override
	public News getNewsByNo(int no) {
		Connection conn = DBConnection.getConnection();
		String sql = "select * from news_table where no=?";
		// 初始化PreparedStatement对象
		PreparedStatement ps = null;
		// 实例化一个News引用
		News news = null;
		// 初始化ResultSet对象
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, no);
			// 得到一个结果集
			rs = ps.executeQuery();
			// 对结果集进行处理
			while (rs.next()) {// next()数组存在则返回true，否则返回false
				// 实例化一个User对象
				news = new News();
				news.setNo(rs.getInt("no"));
				news.setTitle(rs.getString("title"));
				news.setContent(rs.getString("content"));
				news.setAuthor(rs.getString("author"));
				news.setCreatTime(rs.getString("createTime"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭流对象
			CloseObject.close(conn, ps, rs);
		}
		return news;
	}

}
